﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using ERF;
using ERF.Menu;
using FirstPerson.Gametypes;

namespace FirstPerson.States
{
    class JoinState : State
    {
        /// <summary>
        /// Singleton instance
        /// </summary>
        private static JoinState instance;
        private MenuManager menuManager;
        private Menu menu;
        private MenuItem updateButton;
        private TimeSpan currentTimeSpan;
        private bool search = false;

        /// <summary>
        /// Get an instance of this state.
        /// </summary>
        /// <param name="machine">the state machine that this state should belong to.</param>
        /// <returns></returns>
        public static JoinState GetInstance(StateMachine machine)
        {
            if (instance == null)
                instance = new JoinState(machine);

            return instance;
        }

        /// <summary>
        /// Constructor of Joinstate.
        /// </summary>
        /// <param name="machine">the state machine that this state should belong to.</param>
        /// <returns></returns>
        private JoinState(StateMachine machine)
            : base(machine)
        {

        }

        public override void Initialize()
        {
            menu = new Menu("BattleSqudron project");
            menu.MenuItems.Add(new MenuItem("Back", this.BackToStartMenu));
            updateButton = new MenuItem("Update list", this.UpdateList);
            menu.MenuItems.Add(updateButton);
            menu.SelectedIndex = 0;

            menuManager = MenuManager.GetInstance(this.machine.CurrentGame);
            menuManager.Menu = menu;

            search = true;
        }

        private void BackToStartMenu()
        {
            machine.CurrentState = StartMenuState.GetInstance(machine);
        }

        private void UpdateList()
        {
            AvailableNetworkSessionCollection sessions = this.machine.CurrentGame.NetworkHandler.FindGames();
            menu.MenuItems.RemoveRange(2,menu.MenuItems.Count-2);
            foreach (AvailableNetworkSession session in sessions)
            {
                menu.MenuItems.Add(new MenuValueItem(session.HostGamertag + "'s game", session, this.SelectGame));
            }
            menuManager.Menu = menu;
        }

        private void SelectGame()
        {
            AvailableNetworkSession session = (AvailableNetworkSession)(((MenuValueItem)this.menu.SelectedItem).Value);

            PlayingState playingState = PlayingState.GetInstance(machine);
            machine.CurrentState = playingState;

            this.machine.CurrentGame.NetworkHandler.JoinGame(session);
        }

        public void JoinGame(GameData matchData)
        {
            matchData.startTime = currentTimeSpan.TotalSeconds - matchData.startTime;

            PlayingState playingState = PlayingState.GetInstance(machine);
            playingState.initMatch(matchData);

            menuManager.Visible = false;

            MediaPlayer.Stop();
        }

        public override void Update(GameTime gameTime)
        {
            if (search)
            {
                UpdateList();
                search = false;
            }

            currentTimeSpan = gameTime.TotalRealTime;

            if (input.IsButtonReleased("MENU_DOWN"))
            {
                menuManager.Menu.SelectNext();
            }

            if (input.IsButtonReleased("MENU_UP"))
            {
                menuManager.Menu.SelectPrevious();
            }

            if (input.IsButtonReleased("MENU_SELECT"))
            {
                menuManager.Menu.Submit();
            }

            if (input.IsButtonReleased("MENU_BACK"))
                BackToStartMenu();
        }
    }
}
